﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExtraClub.Domain.Entities;
using System.Web.Security;
using ExtraClub.Domain;
using SubSonic;
using System.Transactions;
using ExtraClub.Domain.Framework;
using LB.Generics;

namespace ExtraClub.Web.Forms.Service {
    public class mail_message : BaseWS {
        [WebMethod(true)]
        public string GetTemplate(long templateId) {
            return MailTemplate.FetchByID(templateId).Body;
        }

        #region CREATES RTF MAIL IMAGE AND PUTS IT IN MAIL QUEUE
        protected override void Add(Dictionary<string, object> data) {
            if (!string.IsNullOrEmpty(data["message"].ToString())) {
                MailJob job = new MailJob();
                job.Subject = data["desc"].ToString();
                job.EntityId = int.Parse(data[Constants.FormField.EntityId].ToString());

                Query q = VwClubProfile.Query();
                q.AddWhere(VwClubProfile.Columns.Id, job.EntityId);

                //VwClubProfileCollection all = new VwClubProfileCollection();
                //all.LoadAndCloseReader(VwClubProfile.FetchByQuery(q));

                job.Message = data["message"].ToString();

                job.Created = DateTime.Now;
                job.Save((Guid)Membership.GetUser().ProviderUserKey);

                if (data["save-as-template"].ToString() == "True") {
                    MailTemplate mt = new MailTemplate();
                    mt.Title = job.Subject;
                    mt.Body = job.Message;
                    mt.Created = DateTime.Now;
                    mt.Save((Guid)Membership.GetUser().ProviderUserKey);
                }
            } else {
                throw new Exception("Empty message");
            }
        }

        //private string Substitute(VwClubProfile club, string message) {
        //    message = StringExt.Replace(@"\[\[email\]\]",message, club.Email);
        //    message = StringExt.Replace(@"\[\[companyname\]\]", message, club.Name);
        //    message = StringExt.Replace(@"\[\[username\]\]", message, club.UserName);
        //    return message;
        //}

        private void SusspendMail(Dictionary<string, object> data) {
            if (!string.IsNullOrEmpty(data["message"].ToString())) {
                MailJob queue = new MailJob();
                queue.Subject = "";
                queue.EntityId = int.Parse(data[Constants.FormField.EntityId].ToString());
                queue.Message = data["message"].ToString();
                queue.Created = DateTime.Now;
                queue.Save((Guid)Membership.GetUser().ProviderUserKey);
            } else {
                throw new Exception("Empty message");
            }
        }

        #endregion

        #region Routines
        #endregion
    }
}
